<?php

use Biz\cache\wiki\TopicWiki;
use Biz\service\wiki\Wiki;

/**
 * @deprecated
 * 好物集合 wiki列表
 */
class GoodWikiListAction extends ActionPlugin
{
//    const USER_LEVEL = self::LEVEL_REGISTER;
    const USER_LEVEL = self::LEVEL_NOTHING;
    
    public function main()
    {
        
        $params = $this->getRequest()->getPost();
        //wiki id集合
        $wiki_ids = $params['wiki_ids'] ? explode(',', $params['wiki_ids']) : [];
        //话题 集合
//        $top_ids = $params['top_ids'] ? explode(',', $params['top_ids']): [];
        $top_id = $params['top_ids'] ?: 0;
        //当前页码
        $current_page = $params['current_page'] ?? 1;
        //每页条数
        $per_page = $params['per_page'] ?? 20;
    
        $uid = $this->uid;
        //获取缓存wiki_ids
        $cache_wiki_ids = TopicWiki::getAllWikiIds($top_id);
        //总wiki_ids
        $all_wiki_ids = array_merge($wiki_ids, $cache_wiki_ids);
        
        //总条数
        $count = count($all_wiki_ids);
        
        //偏移量，当前页-1乘以每页显示条数
        $start = ($current_page - 1) * $per_page;
        //每页的wiki_id集合
        $wiki_ids_page = array_slice($all_wiki_ids, $start, $per_page);
        
        //计算当前页数据量
        $current = $current_page * $per_page;
        $is_over = ($count <= $current) ? 1 : 0;
        
        $data['total'] = $count;
        $data['is_over'] = $is_over;
        $data['current_page'] = $current_page;
        $data['per_page'] = $per_page;
        
        $list = Wiki::getWikiBaseList($wiki_ids_page, $uid) ?? [];
        if ($list) {
            foreach ($list as $k => $v) {
                if (in_array($v['id'], $wiki_ids)) {
                    $list[$k]['is_hot'] = 1;
                }
            }
        }
        $data['list'] = array_values($list);
        $this->setSuccess($data);
    }
}